-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.c
76 lines (63 loc) · 1.64 KB
/
Solution.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// A queue node
struct QueueNode {
int vertex;
int distance;
};
// Create a queue node
struct QueueNode createNode(int vertex, int distance) {
struct QueueNode node;
node.vertex = vertex;
node.distance = distance;
return node;
}
// Snake and ladder problem
int minDiceThrows(int board[], int N) {
int visited[N];
for (int i = 0; i < N; i++) {
visited[i] = 0;
}
struct QueueNode queue[N];
int front = 0, rear = 0;
queue[rear++] = createNode(0, 0); // Start from the first cell
visited[0] = 1;
while (front < rear) {
struct QueueNode current = queue[front++];
int v = current.vertex;
if (v == N - 1) {
return current.distance; // Reached the last cell
}
for (int dice = 1; dice <= 6 && v + dice < N; dice++) {
int next = v + dice;
if (!visited[next]) {
visited[next] = 1;
struct QueueNode nextNode = createNode(
board[next] == -1 ? next : board[next],
current.distance + 1);
queue[rear++] = nextNode;
}
}
}
return INT_MAX; // No solution
}
int main() {
int N = 30;
int board[N];
for (int i = 0; i < N; i++) {
board[i] = -1;
}
// Example: Add ladders
board[2] = 21;
board[4] = 7;
board[10] = 25;
board[19] = 28;
// Example: Add snakes
board[26] = 0;
board[20] = 8;
board[16] = 3;
board[18] = 6;
printf("Minimum dice throws required: %d\n", minDiceThrows(board, N));
return 0;
}